//
//  ZCHarmknowVC.m
//  zctx
//
//  Created by apple on 16/1/20.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCHarmknowVC.h"
#import "ZCHarmknowCell.h"
#import "ZCHarmClassifyCell.h"
#import "ZCHarmknowDetailVC.h"

@interface ZCHarmknowVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UITableView *classTableView;

@property (nonatomic,strong)NSMutableArray *classArray;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *totalArray;
@property (nonatomic,assign)NSInteger *offset;
@end

@implementation ZCHarmknowVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCHarmknowVC"];
    [MTA trackPageViewBegin:@"ZCHarmknowVC"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCHarmknowVC"];
    [MTA trackPageViewEnd:@"ZCHarmknowVC"];
}
-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    _offset = 0;
    self.navigationItem.title = @"食品危害须知";
    self.view.backgroundColor =[UIColor colorWithHexString:@"#ebeded"];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0,0.4 *SCREEN_WIDTH,0, 0));
    }];
    
    [self.view addSubview:self.classTableView];
    [self.classTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(64,0,-64, 0.6 *SCREEN_WIDTH));
    }];
 
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCHarmknowCell" bundle:nil] forCellReuseIdentifier:@"harmknowCell"];
    
    [self requestData];

}
-(void)buttonClick{
    NSLog(@"sdf");
}

#pragma mark 我的数据
-(void)requestData{
    
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    [NetworkManager requestIndexgetharmlistWithsuccess:^(NSDictionary *dictResponse) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]){
            NSArray *sarray = (NSArray *)[dictResponse objectForKey:@"content"];
            if (sarray.count>0) {
                for (NSDictionary *dict in sarray) {
                    NSString *title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"item"]];
                    [self.classArray addObject:title];
                    
                    if ([[dict objectForKey:@"item_detial"]isKindOfClass:[NSArray class]]) {
                        NSArray *object = (NSArray *)[dict objectForKey:@"item_detial"];
                        if (object.count>0) {
                            [self.totalArray addObject:object];
                        }else{
                            NSArray *array= [[NSArray alloc]init];
                            [self.totalArray addObject:array];
                        }
                    }else{
                        NSArray *array= [[NSArray alloc]init];
                        [self.totalArray addObject:array];
                    }
                }
                if (self.totalArray.count>0) {
                    self.dataArray =(NSMutableArray *) self.totalArray[0];
                }
                [self.classTableView reloadData];
                [self.tableView reloadData];
                if (self.classArray.count>0) {
                    [self.classTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
                    [self tableView:self.classTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
                }
            }
        }
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        self.bannerView.hidden = NO;
    }];
}
-(void)initTableView{
    }
#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView ==self.tableView) {
        return self.dataArray.count;
    }else{
        return self.classArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"harmknowCell";
    static NSString *classIndentifier = @"harmClassCell";
    if (tableView == self.tableView) {
        ZCHarmknowCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil) {
            cell = [ZCHarmknowCell showZCHarmknowCell];
            
        }
        NSDictionary *dict =(NSDictionary *)self.dataArray[indexPath.row];
        cell.titleLable.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"title"]];
        return cell;
    }else{
        ZCHarmClassifyCell *cell = [tableView dequeueReusableCellWithIdentifier:classIndentifier];
        if (cell == nil) {
            cell = [[ZCHarmClassifyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:classIndentifier];
        }
        cell.titleLable.text = self.classArray[indexPath.row];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSDictionary *dict =(NSDictionary *)self.dataArray[indexPath.row];
        //    NSLog(@"dict = %@",dict);
        ZCHarmknowDetailVC *vc = [[ZCHarmknowDetailVC alloc] init];
        vc.newsId = [NSString stringWithFormat:@"%@",[dict objectForKey:@"id"]];;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        NSLog(@"self = %ld",self.totalArray.count);
        self.dataArray = [self.totalArray[indexPath.row] mutableCopy];
        [self.tableView reloadData];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark 初始化

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.sectionFooterHeight = 0.0001;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        _tableView.separatorColor = RGB(236, 237, 238);
    }
    return _tableView;
}

-(UITableView *)classTableView{
    if (!_classTableView) {
        _classTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _classTableView.delegate = self;
        _classTableView.dataSource = self;
        _classTableView.tableFooterView = [[UIView alloc]init];
        _classTableView.sectionFooterHeight = 0.0001;
        _classTableView.backgroundColor = [UIColor colorWithHexString:@"#F1F1F1"];
        _classTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _classTableView;
}

-(NSMutableArray *)classArray{
    if (!_classArray) {
        _classArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _classArray;
}


-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}

-(NSMutableArray *)totalArray{
    if (!_totalArray) {
        _totalArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _totalArray;
}



@end
